<?php
include('../../includes/classes/core.php');
$start  	= $_REQUEST['start'];
$end    	= $_REQUEST['end'];
$count 		= $_REQUEST["count"];
$action 	= $_REQUEST['act'];
$departament= $_REQUEST['departament'];
$type       = $_REQUEST['type'];
$category   = $_REQUEST['category'];
$s_category = $_REQUEST['sub_category'];
$done 		= $_REQUEST['done']%2;
$name 		= $_REQUEST['name'];
$title 		= $_REQUEST['title'];
$text[0] 	= "შემოსული  ზარები ტრანზაქციის ფორმის მიხედვით ";
$text[1] 	= "'$type'- ტრანზაქციის ფორმების მიხედვით";
$text[2] 	= "'$type'- ტრანზაქციის ფორმების მიხედვით";
$c="3 or incomming_call.call_type_id=0";
if ($type=="თანხის შემოტანა")  $c=421;
elseif ($type=="თანხის გატანა") $c=422;


//------------------------------------------------query-------------------------------------------
switch ($done){
	case  1:
		$result = mysql_query("	SELECT pay_type.`name`,
											COUNT(*),
											CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*)
												FROM 	incomming_call
												JOIN 	pay_type ON pay_type.id=incomming_call.pay_type_id
												WHERE (incomming_call.call_category_id=$c)  and DATE(`incomming_call`.`date`) >= '$start' and  DATE(`incomming_call`.`date`) <= '$end'
												)*100,2),'%')
									FROM 	incomming_call
									JOIN 	pay_type ON pay_type.id=incomming_call.pay_type_id
									WHERE (incomming_call.call_category_id=$c) and DATE(`incomming_call`.`date`) >= '$start' and  DATE(`incomming_call`.`date`) <= '$end'
									GROUP BY pay_type.`name`");
		$text[0]=$text[1];
	break;
	default:
		$result = mysql_query("SELECT category.`name`,
										COUNT(*),
										CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM incomming_call
										JOIN category ON category.id=incomming_call.call_category_id 
										WHERE DATE(`incomming_call`.`date`) >= '$start' AND DATE(`incomming_call`.`date`) <= '$end')*100,2),'%') AND incomming_call.call_category_id>=421 AND incomming_call.call_category_id<=422
								FROM 	incomming_call
								JOIN category ON category.id=incomming_call.call_category_id 
								WHERE 	DATE(`incomming_call`.`date`) >= '$start' and  DATE(`incomming_call`.`date`) <= '$end' AND incomming_call.call_category_id>=421 AND incomming_call.call_category_id<=422
								GROUP BY 	category.`name`");

		break;
}
///----------------------------------------------act------------------------------------------
switch ($action) {
	case "get_list":
		$data = array("aaData"	=> array());
		while ( $aRow = mysql_fetch_array( $result ) )
		{	$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[0] = '0';

				$row[$i+1] = $aRow[$i];
			}
			$data['aaData'][] =$row;
		}
		echo json_encode($data); return 0;
		break;
	case 'get_category' :
		$rows = array();
		while($r = mysql_fetch_array($result)) {
			$row[0] = $r[0];
			$row[1] = (float) $r[1];
			$rows['data'][]=$row;
		}
		$rows['text']=$text[0];
		echo json_encode($rows);
		break;
		case 'get_in_page':
			mysql_query("SET @i = 0;");
			$rResult = mysql_query("SELECT  		inc.id,    
                        						inc.id, 
                        						DATE_FORMAT(inc.`date`,'%d/%m/%y %H:%i:%s') AS `date`,
                        						inc.`pin`,
                        						inc.`pid`,
                        						inc.phone,
                        						category.`name`,
                        						`crystal_users`.`name` ,
                        						`crystal_users1`.`name` ,
                        						if(ISNULL(task.incomming_call_id), 'დასრულებული', `status`.`call_status`)AS `status`,	
                      							TIME_FORMAT(SEC_TO_TIME(asterisk_incomming.duration - 1), '%i:%s') AS `duration`,
												CONCAT('<button class=\'download\' str=',asterisk_incomming.file_name,'>მოსმენა</button>')AS `file_name`
								FROM 				incomming_call AS inc
                                LEFT JOIN 	category  ON inc.call_category_id=category.id
                                LEFT JOIN 	task ON task.incomming_call_id=inc.id								
                                LEFT JOIN 	crystal_users ON task.responsible_user_id=crystal_users.id	
                                LEFT JOIN 	crystal_users   AS `crystal_users1`	ON inc.user_id=crystal_users1.id
                                LEFT JOIN 	`status` ON task.`status`=`status`.id
								LEFT JOIN asterisk_incomming ON inc.uniqueid=asterisk_incomming.uniqueid
								LEFT JOIN 	`pay_type` ON inc.`pay_type_id`=`pay_type`.id
								
				
												WHERE DATE
												(`inc`.`date`) >= '$start' AND DATE(`inc`.`date`) <= '$end'
												 and pay_type.`name`='$_REQUEST[r2]'  and category.`name`='$_REQUEST[r1]'
									
												GROUP BY inc.id");
					$data = array(
					"aaData"	=> array()
			);
		
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
			$row = array();
			$row1 = array();
				
			for ( $i = 0 ; $i < $count ; $i++ )
			{
			$row[] = $aRow[$i];
			$a=$aRow;
		
			{
		
		}
		}
			$data['aaData'][] = $row;
		}
		echo json_encode($data); return 0;
				break;
	default :
		echo "Action Is Null!";
		break;

}



?>